package com.china08.yxyapi.api.manage.sch.platform.website.section;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController("SectionDelController")
public class SectionDelController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @ApiOperation("删除栏目")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/section/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String schoolId = getProfile(request, response).getSchoolId();
        String sectionId = reqModel.sectionId;

        SwsSection swsSection = swsSectionRepository.findById(sectionId);
        Assert.isTrue(null != swsSection, "不存在要删除的栏目");

        String pid = swsSection.getPid();
        // 判断是否存储二级栏目
        if (pid.equals("0")) {
            String id = swsSection.getId();
            List<SwsSection> section2List = swsSectionRepository.findBySchoolIdAndPidOrderByOrderNumAsc(schoolId, id);
            Assert.isTrue(section2List.size() == 0, "有子栏目，请先删除子栏目");
        }

        // 删除一级栏目,将所有一级栏目下面的文章一级栏目id，二级栏目id设置为空
        if (pid.equals("0")) {
            List<SwsArticle> list = swsArticleRepository.findBySection1Id(sectionId);
            for (SwsArticle swsArticle : list) {
                swsArticle.setSection1Id("");
                swsArticle.setSection2Id("");
                swsArticleRepository.save(swsArticle);
            }
        }
        // 删除二级栏目, 将文章二级栏目id设置为空
        else {
            List<SwsArticle> list = swsArticleRepository.findBySection2Id(sectionId);
            for (SwsArticle swsArticle : list) {
                swsArticle.setSection2Id("");
                swsArticleRepository.save(swsArticle);
            }
        }

        //
        swsSectionRepository.delete(swsSection);

        return true;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String sectionId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(sectionId), "未获取到要删除的栏目");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
